import Vue from 'vue'
import { login, logout, phoneLogin, registerWxLogin } from "@/api/login"
import { ACCESS_TOKEN, USER_NAME, USER_INFO, USER_AUTH, SYS_BUTTON_AUTH } from "@/store/mutation-types"
import { welcome } from "@/utils/util"
import { queryPermissionsByUser } from '@/api/api'
import { getAction } from '@/api/manage'

const user = {
  state: {
    token: '',
    username: '',
    firstName: '',
    welcome: '',
    avatar: '',
    permissionList: [],
    info: {},
    buData: '',
    departmentId: '',
    sevenDayCrmBusinessNum: 0,
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, { username, firstName, welcome }) => {
      state.username = username
      state.firstName = firstName
      state.welcome = welcome
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_PERMISSIONLIST: (state, permissionList) => {
      state.permissionList = permissionList
    },
    SET_INFO: (state, info) => {
      state.info = info
    },
    SET_BUDATA: (state, buData) => {
      state.buData = buData
    },
    SET_DEPARTMENTID: (state, departmentId) => {
      state.departmentId = departmentId
    },
    SET_SEVENDAYBUSINESSNUM: (state, num) => {
      state.sevenDayCrmBusinessNum = num
    },
  },

  actions: {
    // CAS验证登录
    ValidateLogin({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        getAction("/cas/client/validateLogin", userInfo).then(response => {
          console.log("----cas 登录--------", response);
          if (response.success) {
            const result = response.result
            const userInfo = result.userInfo
            Vue.ls.set('Access-Token', result[ACCESS_TOKEN], 7 * 24 * 60 * 60 * 1000)

            Vue.ls.set(ACCESS_TOKEN, result[ACCESS_TOKEN], 7 * 24 * 60 * 60 * 1000)
            Vue.ls.set(USER_NAME, userInfo.firstName, 7 * 24 * 60 * 60 * 1000)
            Vue.ls.set(USER_INFO, userInfo, 7 * 24 * 60 * 60 * 1000)
            commit('SET_TOKEN', result[ACCESS_TOKEN])
            commit('SET_INFO', userInfo)
            commit('SET_NAME', { username: userInfo.userLoginId, firstName: userInfo.firstName, welcome: welcome() })
            commit('SET_AVATAR', userInfo.logoImageUrl)
            commit('SET_DEPARTMENTID', userInfo.departmentId)
            resolve(response)
          } else {
            resolve(response)
          }
        }).catch(error => {
          reject(error)
        })
      })
    },
    //扫码登陆
    registerWxLogin({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        registerWxLogin(userInfo).then(response => {
          if (response.code == '0') {
            const result = response.result
            const userInfo = result.userInfo
            Vue.ls.set('Access-Token', result[ACCESS_TOKEN], 7 * 24 * 60 * 60 * 1000)
            Vue.ls.set(ACCESS_TOKEN, result[ACCESS_TOKEN], 7 * 24 * 60 * 60 * 1000)
            Vue.ls.set(USER_NAME, userInfo.firstName, 7 * 24 * 60 * 60 * 1000)
            Vue.ls.set(USER_INFO, userInfo, 7 * 24 * 60 * 60 * 1000)
            commit('SET_TOKEN', result[ACCESS_TOKEN])
            commit('SET_INFO', userInfo)
            commit('SET_NAME', { username: userInfo.userLoginId, firstName: userInfo.firstName, welcome: welcome() })
            commit('SET_AVATAR', userInfo.logoImageUrl)
            commit('SET_DEPARTMENTID', userInfo.departmentId)
            resolve(response)
          } else {
            reject(response)
          }
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 登录
    Login({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        login(userInfo).then(response => {
          if (response.code == '0') {
            const result = response.result
            const userInfo = result.userInfo
            Vue.ls.set('Access-Token', result[ACCESS_TOKEN], 7 * 24 * 60 * 60 * 1000)
            Vue.ls.set(ACCESS_TOKEN, result[ACCESS_TOKEN], 7 * 24 * 60 * 60 * 1000)
            Vue.ls.set(USER_NAME, userInfo.firstName, 7 * 24 * 60 * 60 * 1000)
            Vue.ls.set(USER_INFO, userInfo, 7 * 24 * 60 * 60 * 1000)
            commit('SET_TOKEN', result[ACCESS_TOKEN])
            commit('SET_INFO', userInfo)
            commit('SET_NAME', { username: userInfo.userLoginId, firstName: userInfo.firstName, welcome: welcome() })
            commit('SET_AVATAR', userInfo.logoImageUrl)
            commit('SET_DEPARTMENTID', userInfo.departmentId)
            getAction('/online/api/getNotUpdateBusiness', { mobile: userInfo.userLoginId }).then((res) => {
              if (res.success) {
                this.$store.dispatch("getSevenDayBusinessNum", res.result.sevenDayCrmBusinessNum);
              }
            })
            resolve(response)
          } else {
            reject(response)
          }
        }).catch(error => {
          reject(error)
        })
      })
    },
    //手机号登录
    PhoneLogin({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        phoneLogin(userInfo).then(response => {
          if (response.code == '0') {
            const result = response.result
            const userInfo = result.userInfo
            Vue.ls.set('Access-Token', result[ACCESS_TOKEN], 7 * 24 * 60 * 60 * 1000)
            Vue.ls.set(ACCESS_TOKEN, result[ACCESS_TOKEN], 7 * 24 * 60 * 60 * 1000)
            Vue.ls.set(USER_NAME, userInfo.firstName, 7 * 24 * 60 * 60 * 1000)
            Vue.ls.set(USER_INFO, userInfo, 7 * 24 * 60 * 60 * 1000)
            commit('SET_TOKEN', result[ACCESS_TOKEN])
            commit('SET_INFO', userInfo)
            commit('SET_NAME', { username: userInfo.userLoginId, firstName: userInfo.firstName, welcome: welcome() })
            commit('SET_AVATAR', userInfo.logoImageUrl)
            commit('SET_DEPARTMENTID', userInfo.departmentId)
            resolve(response)
          } else {
            reject(response)
          }
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 获取用户信息
    GetPermissionList({ commit }) {
      return new Promise((resolve, reject) => {
        let params = { parentId: 'CRM_ROOT' };
        queryPermissionsByUser(params).then(response => {
          const menuData = response.result.menu;
          const authData = response.result.auth;
          const allAuthData = response.result.allAuth;
          //Vue.ls.set(USER_AUTH,authData);
          sessionStorage.setItem(USER_AUTH, JSON.stringify(authData));
          sessionStorage.setItem(SYS_BUTTON_AUTH, JSON.stringify(allAuthData));
          let buData = ''
          authData.forEach(item => {
            if (item.action == 'allhrdata') {
              buData = 'allhrdata'
            }
          })
          commit('SET_BUDATA', buData)
          if (menuData && menuData.length > 0) {
            //update--begin--autor:qinfeng-----date:20200109------for：JEECG-63 一级菜单的子菜单全部是隐藏路由，则一级菜单不显示------
            menuData.forEach((item, index) => {
              if (item["children"]) {
                let hasChildrenMenu = item["children"].filter((i) => {
                  return !i.hidden || i.hidden == false
                })
                if (hasChildrenMenu == null || hasChildrenMenu.length == 0) {
                  item["hidden"] = true
                }
              }
            })
            console.log(" menu show json ", menuData)
            //update--end--autor:qinfeng-----date:20200109------for：JEECG-63 一级菜单的子菜单全部是隐藏路由，则一级菜单不显示------
            commit('SET_PERMISSIONLIST', menuData)
          } else {
            reject('getPermissionList: permissions must be a non-null array !')
          }
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 登出
    Logout({ commit, state }) {
      return new Promise((resolve) => {
        let logoutToken = state.token;
        commit('SET_TOKEN', '')
        commit('SET_INFO', {})
        commit('SET_PERMISSIONLIST', [])
        commit('SET_DEPARTMENTID', '')
        Vue.ls.remove(ACCESS_TOKEN)
        //console.log('logoutToken: '+ logoutToken)
        logout(logoutToken).then(() => {
          //var sevice = "http://"+window.location.host+"/";
          //var serviceUrl = encodeURIComponent(sevice);
          //window.location.href = window._CONFIG['casPrefixUrl']+"/logout?service="+serviceUrl;
          resolve()
        }).catch(() => {
          resolve()
        })
      })
    },
    getSevenDayBusinessNum({commit},num) {
      commit('SET_SEVENDAYBUSINESSNUM',num)
    }
  }
}

export default user